import { INestApplication } from '@nestjs/common';
import { RedisService } from '../../src/auth/redis/redis.service';
import { RedisModule } from '../../src/auth/redis/redis.module';

// Global variable to store the Redis service instance
export let redisService: RedisService | null = null;

// Store the Redis service for later cleanup
export function storeRedisService(app: INestApplication) {
  try {
    // If we already have a Redis service, don't overwrite it
    if (!redisService) {
      try {
        // Get the Redis service from the Redis module
        redisService = app.select(RedisModule).get(RedisService, { strict: false });
        if (redisService) {
          console.log('Redis service stored for global teardown');
        } else {
          console.log('Redis service not found in the application context');
        }
      } catch (error) {
        console.log('Failed to get RedisService from module:', error.message);
      }
    }
  } catch (error) {
    console.log('Error in storeRedisService:', error.message);
  }
}
